<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * Upload image in folder and create thumbnail from
 *
   USAGE:
   <pre>

   $RecordImageUpload = $this->CoreModel('RecordImageUpload');

   $params  = array('image_folder_path'        => string,  // folder name of record media files
                    'source_image_folder_path' => string   // full path to the source image directory
                    );

   $result  = $RecordImageUpload->run( $params );


   </pre>
 * @package GeoContexter
 * @subpackage Module_Geocontexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 768 $ / $LastChangedDate: 2010-12-16 16:11:56 +0100 (jeu., 16 déc. 2010) $ / $LastChangedBy: armand.turpel $
 */

namespace Geocontexter\Model;

use Core\Model\AbstractModel;
use Core\Model\InterfaceModel;

class RecordImageUpload extends    AbstractModel
                        implements InterfaceModel
{
    /**
     * upload image
     */
    public function run( $params )
    {

		$this->create_files_folder( $params['image_folder_path'] );
		$this->move_images( $params['source_image_folder_path'], $params['image_folder_path'] );

		if (isset($params['delete_source'])) {
			$this->delete_source($params['source_image_folder_path']);
		}
    }

    /**
     * upload lists backup file for import
     */
    public function move_images($source, $dest)
    {
		foreach (
		 $iterator = new \RecursiveIteratorIterator(
		  new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
		  \RecursiveIteratorIterator::SELF_FIRST) as $item
		) {
		  if ($item->isDir() && !is_dir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName())) {
			mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
		  } else if ($item->isFile()){
			copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
		  }
		}
    }

	public static function delete_source($dir) {
		$iterator = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
		foreach (new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
			  if (!$file->isDir()) {
				 unlink($file->getPathname());
			  }
		}
	}

    /**
     * validate parameters
     *
     * @param array $params
     */
    private function validate_params( & $params )
    {

    }

    /**
     *
     */
    private function create_files_folder( $path )
    {
        if (!is_dir($path)) {

            $oldumask = umask(0);

            if (false === mkdir($path, 0776)) {
                umask($oldumask);
                throw new \Exception('Couldnt create directory: ' . $path);
            }
            if (false === mkdir($path . '/thumbnail', 0776)) {
                umask($oldumask);
                throw new \Exception('Couldnt create directory: ' . $path . '/thumbnail');
            }

            umask($oldumask);
        }
    }

}